package com.qys.livesMall.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qys.livesMall.app.user.service.IH2AppUserService;
import com.qys.livesMall.common.constants.Constants;
import com.qys.livesMall.shop.constants.OrderStatus;
import com.qys.livesMall.shop.controller.app.AppShopOrderCommentController;
import com.qys.livesMall.shop.modal.dto.OrderCommentDto;
import com.qys.livesMall.shop.modal.dto.shop.ShopScoreDTO;
import com.qys.livesMall.shop.modal.entity.ShopOrderInfo;
import com.qys.livesMall.shop.modal.entity.ShopOrderSku;
import com.qys.livesMall.shop.open.navy.mapper.NavyTaskOpenMapper;
import com.qys.livesMall.shop.service.*;
import com.qys.livesMall.shop.task.ShopTaskInter;
import com.qys.livesMall.system.service.IH2SysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @menu ShopAutoShippedServiceImpl
 * @date 2023/10/23 15:11
 */
@Slf4j
@Service
public class ShopAutoReceivedServiceImpl implements ShopAutoReceivedService {

    @Autowired
    private IH2SysConfigService sysConfigService;

    @Autowired
    private ShopOrderInfoService shopOrderInfoService;

    @Autowired
    private ShopOrderSkuService shopOrderSkuService;

    @Autowired
    private IH2AppUserService userService;

    @Autowired
    private ShopTaskInter shopTaskInter;

    @Autowired
    private AppShopOrderCommentController commentController;

    @Autowired
    private NavyTaskOpenMapper navyTaskOpenMapper;

    @Override
    public void createTask() {
        log.info("自动收货开始....");
        int outTime=1;
        String time=sysConfigService.getConfigKey("received_out_time");
        if (Strings.isBlank(time)){
            outTime=48;
        }else {
            outTime=Integer.parseInt(time);
        }
        long start=System.currentTimeMillis()-outTime*3600*1000;
        long end =start+3600*1000;
        List<ShopOrderSku> outOrderSkus = shopOrderSkuService.getReceivedOutTimeOrder(0, end);
        if (CollectionUtils.isEmpty(outOrderSkus)){
            log.info("自动收货结束,共收货0单");
            return;
        }
        for(ShopOrderSku order:outOrderSkus){

            if (!userService.userIsNavy(order.getUserId().toString())){
                continue;
            }
            //触发时间
            Long eventLong=(order.getConsignTime()+outTime*3600*1000+5000-System.currentTimeMillis())/1000;
            if (eventLong<=0){
                eventLong=(long)(Math.random()*3600);
            }
            int eventTime =Integer.valueOf(eventLong.toString());
            log.info("订单{}距收货时间还有:{}s",order.getId(),eventTime);
            shopTaskInter.receivedTask(eventTime,order.getId());
        }
    }

    public void exc(Long orderSkuId){
        ShopOrderSku orderSku = shopOrderSkuService.getById(orderSkuId);
        if (orderSku == null) {
            return;
        }
        shopOrderInfoService.operation(orderSkuId);
        //收货后进行商品评论
        try{comment(orderSku);}catch (Exception e){
            log.info("评论商品失败："+e);
        }
        log.info("订单{}距收货结束",orderSkuId);
    }

    private void comment(ShopOrderSku orderSku){
        OrderCommentDto orderCommentDto=new OrderCommentDto();
        orderCommentDto.setOrderId(orderSku.getOrderId().toString());
        orderCommentDto.setParentId("0");
        orderCommentDto.setRating("1");
        orderCommentDto.setContent(navyTaskOpenMapper.getNavyGoodsComment());
        ShopScoreDTO scoreDTO=new ShopScoreDTO();
        scoreDTO.setShopId(orderSku.getShopId());
        scoreDTO.setConsultScore(new BigDecimal("4.5"));
        scoreDTO.setProductScore(new BigDecimal("4.5"));
        scoreDTO.setLogisticsScore(new BigDecimal("4.5"));
        scoreDTO.setAfterSaleScore(new BigDecimal("4.5"));
        orderCommentDto.setShopScore(scoreDTO);
        commentController.add(Arrays.asList(orderCommentDto));
    }
}

